package com.huiyujinrong.qiangdanbao.activity.my;


import com.huiyujinrong.qiangdanbao.BorrowHelpApp;
import com.huiyujinrong.qiangdanbao.DaggerModelComponent;
import com.huiyujinrong.qiangdanbao.model.FeedbackModel;
import com.huiyujinrong.qiangdanbao.network.Api;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;

import javax.inject.Inject;

import happyloan.core.RespEvent;

public class FeedbackPresenter implements FeedbackContract.Presenter {

    @Inject
    FeedbackModel model;

    private FeedbackContract.View view;

    public FeedbackPresenter(FeedbackContract.View view) {
        this.view = view;
        setDagger2();
    }

    private void setDagger2() {
        DaggerModelComponent.builder().appComponent(BorrowHelpApp.get().getAppComponent()).build().inject(this);
    }

    @Override
    public void upLoadFeedback(String user_id, String feedback_content, File file1, File file2, File file3, File file4) {
        view.disPlay();
        model.upLoadFeedback(user_id, feedback_content, file1, file2, file3, file4);
    }

    @Override
    public void start() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void end() {
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void upLoadFeedBack(RespEvent<String> event) {
        view.disMiss();
        if (event.getUrl().equals(Api.upLoadFeedBack)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    view.showToast("反馈成功");
                    view.inOK();
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    if (event.isAlert()) {
                        view.showToast(String.valueOf(event.getExtra()));
                    }
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("因网络原因,提交反馈失败");
                    break;
            }
        }
    }
}
